#ifndef DWARF_H
#define DWARF_H

#include <stdint.h>
#include <stddef.h>
#include <stdio.h>

// DWARF调试信息相关结构
typedef struct {
    uintptr_t address;      // 机器指令地址
    unsigned int line;      // 源码行号
    char filename[256];     // 源文件名
} dwarf_line_info_t;

// 变量类型信息
typedef enum {
    DWARF_TYPE_UNKNOWN = 0,
    DWARF_TYPE_INT,
    DWARF_TYPE_CHAR,
    DWARF_TYPE_FLOAT,
    DWARF_TYPE_DOUBLE,
    DWARF_TYPE_POINTER,
    DWARF_TYPE_ARRAY,
    DWARF_TYPE_STRUCT,
    DWARF_TYPE_UNION
} dwarf_type_kind_t;

typedef struct {
    char name[256];             // 变量名
    dwarf_type_kind_t type;     // 变量类型
    uintptr_t address;          // 变量地址
    size_t size;                // 变量大小（字节）
    char type_name[256];        // 类型名称（如"int", "char*"等）
    char filename[256];         // 定义所在的文件名
    unsigned int line;          // 定义所在的行号
} dwarf_variable_info_t;

struct dwarf_line_table_s {
    dwarf_line_info_t *lines;
    size_t count;
    size_t capacity;
};

struct dwarf_variable_table_s {
    dwarf_variable_info_t *variables;
    size_t count;
    size_t capacity;
};

typedef struct dwarf_line_table_s dwarf_line_table_t;
typedef struct dwarf_variable_table_s dwarf_variable_table_t;

// DWARF解析函数
int dwarf_load_line_info(dwarf_line_table_t *table, const char *program_path);
uintptr_t dwarf_find_address_by_line(dwarf_line_table_t *table, const char *filename, unsigned int line);
int dwarf_find_line_by_address(dwarf_line_table_t *table, uintptr_t address, char *filename, size_t filename_size, unsigned int *line);
void dwarf_line_table_destroy(dwarf_line_table_t *table);

// 变量类型信息函数
int dwarf_load_variable_info(dwarf_variable_table_t *table, const char *program_path);
dwarf_variable_info_t* dwarf_find_variable_by_name(dwarf_variable_table_t *table, const char *variable_name);
dwarf_variable_info_t* dwarf_find_variable_by_address(dwarf_variable_table_t *table, uintptr_t address);
void dwarf_variable_table_destroy(dwarf_variable_table_t *table);

// 高级DWARF解析函数（使用dwarfdump）
int dwarf_load_variable_info_advanced(dwarf_variable_table_t *table, const char *program_path);

#endif // DWARF_H